package com.ffapp.demo.bean;


import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.annotation.JSONField;
import com.ffapp.demo.basis.Constants;
import com.ffapp.demo.jpush.TagAliasOperatorHelper;

import my.FileUtils;
import my.LogUtil;
import my.MySharedPreferences;


public class LoginBean extends BasisBean {
    private static final long serialVersionUID = 1L;

    public static final int ERRO_CODE_DEVICE = -3001;


    public static final int LOGIN_TYPE_PERSON = 0;
    public static final int LOGIN_TYPE_FA = 1;

    public static final String FILE_CACHE = Constants.DIR_FILECACHE
            + "login.data";
    private static LoginBean sLoginBean;

    private String access_token;
    private String refresh_token;
    private String scope;
    private String token_type;
    private String jti;
    private long expires_in;
    private UserInfoBean userInfo;



    public LoginBean() {
        super();
        // sLoginBean = this;
    }

    public static boolean isLogin() {
		return getInstance() != null && !TextUtils.isEmpty(sLoginBean.getAccess_token());
//        return getInstance() != null && getInstance().getUser() != null;
//        return false;
    }

    public static LoginBean getInstance() {
        if (sLoginBean == null)
            sLoginBean = getFromCache();
        return sLoginBean;
    }

    public static LoginBean getFromCache() {
        LoginBean data;
        data = (LoginBean) FileUtils.readObject(FILE_CACHE);
        if (data != null) {
            sLoginBean = data;
        }
        LogUtil.i("login---->getFromCache---" + (data == null));
        return data;
    }

    public void save() {
        sLoginBean = this;
        FileUtils.cacheObject(FILE_CACHE, this);
        // FileSaveHandler.saveObject(FILE_CACHE, this);
        LogUtil.i("login---->save---" + FILE_CACHE);
    }

    private static void delete() {
        FileUtils.cacheObject(FILE_CACHE, null);
        // FileSaveHandler.removeObject(FILE_CACHE);
    }

	public static void logout() {
		delete();
		sLoginBean = null;
        TagAliasOperatorHelper.getInstance().cleanAlias();
	}

    public static void logout(Context mContext) {
        delete();
        sLoginBean = null;
        MySharedPreferences mSp = new MySharedPreferences(mContext);
        mSp.putIsLogined(false);
        mSp.putPassword("");
        mSp.putUser("");
        TagAliasOperatorHelper.getInstance().cleanAlias();
    }

    public static void logPast(Context mContext) {
        delete();
        sLoginBean = null;
        MySharedPreferences mSp = new MySharedPreferences(mContext);
        mSp.putIsLogined(false);
    }

    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }


//    public   String getJpushAlias(){
//        return "ukluser_"+getUserId();
//    }

    public static String getUserToken() {
        if (!isLogin()) return "";
        return getInstance().getAccess_token();
//        return getInstance().getUser().getUserId();
    }
    public static String getRefreshToken() {
        if (!isLogin()) return "";
        return getInstance().getRefresh_token();
    }
    public static String getUserId() {
        if (!isLogin()) return "";
//        return getInstance().getAccess_token();
        return getInstance().getUserInfo().getId();
    }


    @JSONField(serialize = false)
    public String getJpushAlias() {
        return userInfo== null?"":userInfo.getAlias();
    }

    public String getAccess_token() {
        return access_token;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public String getRefresh_token() {
        return refresh_token;
    }

    public void setRefresh_token(String refresh_token) {
        this.refresh_token = refresh_token;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public String getToken_type() {
        return token_type;
    }

    public void setToken_type(String token_type) {
        this.token_type = token_type;
    }

    public String getJti() {
        return jti;
    }

    public void setJti(String jti) {
        this.jti = jti;
    }

    public long getExpires_in() {
        return expires_in;
    }

    public void setExpires_in(long expires_in) {
        this.expires_in = expires_in;
    }

    public UserInfoBean getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfoBean userInfo) {
        this.userInfo = userInfo;
    }
}
